package com.shoppingmall.service.impl;

import com.shoppingmall.common.ServletResponse;
import com.shoppingmall.dao.UserMapper;
import com.shoppingmall.pojo.User;
import com.shoppingmall.service.UserService;
import com.shoppingmall.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Created by Administrator on 2019/3/18 15:39
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 该方法用来校验数据  其中1 校验username    2 校验 email
     * 参数错误返回的data数据为0
     * 如果数据存在，则返回的data数据为1
     * 数据不存在返回的data为6
     * @param value
     * @param type
     * @return
     */
    @Override
    public ServletResponse<Integer> checkValue(String value,Integer type){
        //校验参数是否在正常范围内   如果错误返回0
        if (type == null || (type < 1 && type > 2)){
            return ServletResponse.createByErrorMessageAndData("参数错误",0);
        }
        if (!StringUtils.isNotBlank(value)){
            return ServletResponse.createByErrorMessageAndData("参数错误",0);
        }
        if (type == 1){
            //校验的是username
            int resultCount = userMapper.checkUsername(value);
            if (resultCount == 1) {
                return ServletResponse.createBySuccess("用户名存在",1);
            }else {
                return ServletResponse.createByErrorMessageAndData("用户名不存在,可以使用",6);
            }
        }else{
            //校验的是email
            int resultCount = userMapper.checkEmail(value);
            if (resultCount == 1) {
                return ServletResponse.createBySuccess("邮箱存在",1);
            }else {
                return ServletResponse.createByErrorMessageAndData("邮箱不存在",6);
            }
        }
    }

    /**
     * 处理用户登录逻辑
     * @param username
     * @param password
     */
    @Override
    public ServletResponse<User> userLogin(String username, String password) {
        //先检查这个用户名是否存在
        int chkUsernameCount = checkValue(username, 1).getData();
        if (chkUsernameCount != 1) {
            return ServletResponse.createByErrorMessage("用户名或密码错误");
        }
        User user = userMapper.selectUserByUsernameAndPassword(username, MD5Util.MD5EncodeUtf8(password));
        if (user == null){
            return ServletResponse.createByErrorMessage("用户名或密码错误");
        }
        return ServletResponse.createBySuccess(user);



    }

    @Override
    public ServletResponse userRegister(User user) {
        //首先需要校验数据  校验用户名和邮箱是否存在
        int checkUsernameCount = checkValue(user.getUsername(), 1).getData();
        if (checkUsernameCount == 1){
            return ServletResponse.createByErrorMessage("用户名已经存在");
        }
        int checkEmailCount = checkValue(user.getEmail(), 2).getData();
        if (checkEmailCount == 1){
            return ServletResponse.createByErrorMessage("邮箱已经存在");
        }
        //加密密码。加salt值
        user.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword()));
        //此处需要设置为1 表示普通用户
        user.setRole(1);
        int insertCount = userMapper.insert(user);
        if (insertCount != 1){
            return ServletResponse.createByErrorMessage("用户注册失败");
        }


        return ServletResponse.createBySuccessMessage("用户注册成功");
    }

    @Override
    public ServletResponse getUserQuestionByUsername(String username) {

        User userByUsername = userMapper.selectUserByUsername(username);
        if (null == userByUsername){
            return ServletResponse.createByErrorMessage("获取用户问题失败");
        }


        return ServletResponse.createBySuccess(userByUsername.getQuestion());
    }

    @Override
    public ServletResponse<User> checkUserAnswer(String username, String question, String answer) {
        User userFromAnswer = userMapper.selectUserByUsernameAndAnswer(username,question,answer);
        if (null == userFromAnswer){
            return ServletResponse.createByErrorMessage("答案校验错误");
        }
        //将查询到的数据返回给controller 在controller生成guava缓存

        return ServletResponse.createBySuccess(userFromAnswer);
    }

    @Override
    public ServletResponse resetPasswordByAnswer(String username, String passwordNew) {
        //判断用户名是否存在
        User userFromUsername = userMapper.selectUserByUsername(username);
        if (null == userFromUsername){
            return ServletResponse.createByErrorMessage("用户不存在");
        }
        //对密码进行MD5加密
        String md5Password = MD5Util.MD5EncodeUtf8(passwordNew);
        userFromUsername.setPassword(md5Password);
        int flag = userMapper.updateByPrimaryKeySelective(userFromUsername);
        if (1 != flag){
            return ServletResponse.createByErrorMessage("修改密码失败");
        }


        return ServletResponse.createBySuccess("修改密码成功");
    }

    @Override
    public ServletResponse resetPasswordByPassword(String username, String password, String passwordNew) {
        //校验用户名是否存在
        ServletResponse<Integer> checkUsername = checkValue(username, 1);
        if (1 != checkUsername.getData()){
            return ServletResponse.createByErrorMessage("用户名非法，请查证后重试");

        }
        //根据用户名和密码查询这个用户
        User userByPassword = userMapper.selectUserByUsernameAndPassword(username, MD5Util.MD5EncodeUtf8(password));
        if (null == userByPassword){
            return ServletResponse.createByErrorMessage("原密码输入错误，请重试");

        }
        userByPassword.setPassword(MD5Util.MD5EncodeUtf8(passwordNew));
        int flag = userMapper.updateByPrimaryKeySelective(userByPassword);
        if (1 !=flag){
            return  ServletResponse.createByErrorMessage("修改密码失败");

        }

        return ServletResponse.createBySuccessMessage("修改密码成功");


    }

    @Override
    public ServletResponse updateUserInformation(String username, String email, String phone, String question, String answer) {
        //先查询这个用户
        User userFromUsername = userMapper.selectUserByUsername(username);
        userFromUsername.setEmail(email);
        userFromUsername.setPhone(phone);
        userFromUsername.setQuestion(question);
        userFromUsername.setAnswer(answer);
        int flag = userMapper.updateByPrimaryKeySelective(userFromUsername);
        if (1 != flag){
            return ServletResponse.createByErrorMessage("修改失败，请重试");
        }


        return ServletResponse.createBySuccessMessage("修改成功");
    }
}
